export const CURRENT_CITY_KEY = 'CURRENT_CITY';
// 百度的城市定位构造函数。
const { LocalCity } = window.BMap;

export const getCurrentCity = () => new Promise((ok, err) => {
    const city = localStorage[CURRENT_CITY_KEY];
    city ? ok(JSON.parse(city)) : new LocalCity().get(async result => {
        const res = await (await fetch('http://127.0.0.1:8080/area/info?name=' + result.name)).json()
        // res.status === 200 ? ok(res.body) : err(res);
        if (res.status === 200) {
            localStorage[CURRENT_CITY_KEY] = JSON.stringify(res.body);
            ok(res.body);
        } else err(res)
    })
});